package action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import biz.CustomerBiz;

@Controller
public class ReportAction extends BaseAction {
	@Resource
	private CustomerBiz biz;
	
	public String state(){
		List list1=biz.reportState("新客户");
		List list2=biz.reportState("忠实客户");
		List list3=biz.reportState("流失客户");
		
		StringBuffer sb=new StringBuffer();
		getStr(list1, sb,"blue");
		getStr(list2, sb,"red");
		getStr(list3, sb,"green");
		
		setMsg(sb.toString());
		return "report";
	}

	private void getStr(List list1, StringBuffer sb,String color) {
		sb.append("myChart.setDataArray([");
		for(int i=0;i<list1.size();i++){
			Object[] obj=(Object[]) list1.get(i);
			int month1=Integer.valueOf(obj[0].toString());
			//前在的补全
			if(i==0){
				for(int j=1;j<month1;j++){
					sb.append("["+j+",0]");
						sb.append(",");
				}
			}
			
			
			sb.append("["+month1+","+obj[1].toString()+"]");
			
			//后面的补全
			int month2=13;
			//判断下一个是否还有数据
			if((i+1)<list1.size()){
				Object[] obj2=(Object[]) list1.get(i+1);
				month2=Integer.valueOf(obj2[0].toString());
			}
			
			//判断是否需要补充数据
			if(month2-month1>1){
				sb.append(",");
				for(int j=month1+1;j<month2;j++){
					sb.append("["+j+",0]");
					if(j<month2-1){
						sb.append(",");
					}
				}
				//如果下一个月不是12月份，那后面需要添加逗号
				if(month2<12){
					sb.append(",");
				}
			}else{
				if(i<list1.size()-1){
					sb.append(",");
				}
			}
		}
		sb.append("], '"+color+"');");
	}
}
